package leetcode_501_600;

import node.TreeNode;

public class LeeCode_563 {
    public static void main(String[] args) {
        System.out.println(findTilt(new TreeNode(1, new TreeNode(2), new TreeNode(3))));
    }
    private static int findTilt(TreeNode root) {
        int[] ans = new int[1];
        getSum(root, ans);
        return ans[0];
    }
    private static int getSum(TreeNode node, int[] ans){
        if (node == null)
            return 0;
        int ls = getSum(node.left, ans);
        int rs = getSum(node.right, ans);
        ans[0] += Math.abs(ls - rs);
        return ls + rs + node.val;
    }
}
